package com.base.cn.platform.os.controller.course.profile;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.course.CourseType;
import com.base.cn.platform.os.service.course.catalog.CourseCatalogService;
import com.base.cn.platform.os.service.course.comment.CourseCommentService;
import com.base.cn.platform.os.service.course.cou.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程评价管理
 *
 * @author SK
 * @since 2018-06-19
 **/
@Controller
@RequestMapping("/admin/course/profile")
public class AdminCourseProfileController extends AdminBaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseCatalogService courseCatalogService;
    @Autowired
    private CourseCommentService courseCommentService;

    /**
     * 初始化课程评价列表页面
     *
     * @return 课程评价列表页面
     */
    @RequestMapping("/profileList")
    public String profileList() {
        return resultPage("/course/profile/profile-list");
    }

    /**
     * 课程评价列表页面
     *
     * @return 课程评价列表页面
     */
    @RequestMapping("/ajaxProfileList")
    public String ajaxProfileList(Model model,
                                  @RequestBody Map<String, Object> params,
                                  @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        params.put("type", 1);
        params.put("notCourseTypeKey", CourseType.COURSE_TYPE_PACKAGE.getKey());
        Map<String, Object> dataMap = courseService.findWebCoursePage(params, currentPage, 10, true, true, false, true,false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("course/profile/ajax-profile-list");
    }

    /**
     * 初始化课程评价明细页面
     *
     * @return 课程评价明细页面
     */
    @RequestMapping("/courseProfileDetailList")
    public String courseProfileDetailList(Model model,
                                          @RequestParam("courseId") BigDecimal courseId) {
        model.addAttribute("courseId", courseId);
        return resultPage("/course/profile/course-profile-detail");
    }

    /**
     * 初始化章节评价明细页面
     *
     * @return 章节评价明细页面
     */
    @RequestMapping("/catalogProfileDetailList")
    public String catalogProfileDetailList(Model model,
                                           @RequestParam("catalogId") BigDecimal catalogId) {
        model.addAttribute("catalogId", catalogId);
        return resultPage("/course/profile/catalog-profile-detail-list");
    }

    /**
     * 章节评价明细页面
     *
     * @return 章节评价明细页面
     */
    @RequestMapping("/ajaxCatalogProfileDetailList")
    public String ajaxCatalogProfileDetailList(Model model,
                                               @RequestBody Map<String, Object> condition,
                                               @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        Map<String, Object> dataMap = courseCommentService.findCourseCommentPage(condition, currentPage, null, true, false, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/course/profile/ajax-catalog-profile-detail-list");
    }

    /**
     * 更新课程评价状态
     *
     * @param commentIds 课程评价id
     * @param status     1.显示 2.隐藏
     * @return 操作结果
     */
    @RequestMapping("/updateCourseCommentStatus")
    @ResponseBody
    public Map<String, Object> updateCourseCommentStatus(@RequestParam("commentIds") String commentIds,
                                                         @RequestParam("status") Integer status) {
        return courseCommentService.updateCourseCommentStatus(commentIds, status);
    }

    /**
     *
     *导出评价明细
     * @param id 课程的id,或者被评价章节的id
     * @param commentIds
     */
    @RequestMapping("/downloadExcelDetailList")
    @OpLog(module = AdminModuleEnum.OTHER, action = "导出评价明细")
    public ResponseEntity<byte[]> downloadExcelDetailList(@RequestParam(value = "courseId" , required = false) String courseId,@RequestParam(value = "id" , required = false) String id,@RequestParam(value = "commentIds" , required = false) String commentIds,@RequestParam("type") String type,@RequestParam(value ="endPayCommentTime",required = false) String endPayCommentTime,@RequestParam(value = "beginCommentTime",required = false) String beginCommentTime,@RequestParam(value ="gainDay",required = false) Integer gainDay) throws Exception {
        Map<String,Object> map  =new HashMap<String,Object>();

        if(ObjectUtils.isNotEmpty(courseId)){
            map.put("courseId",courseId);
        }
        if(ObjectUtils.isNotEmpty(id)){
            map.put("id",id);
        }
        map.put("commentIds",commentIds);
        map.put("beginCommentTime",beginCommentTime);
        map.put("endPayCommentTime",endPayCommentTime);
        map.put("type",type);
        map.put("gainDay",gainDay);
        return courseCatalogService.downloadExcelDetailList(map);

    }
    /**
     * 初始化章节评价明细页面
     *
     * @return 章节评价明细页面
     */
    @RequestMapping("/courseCatalogProfileDetail")
    public String courseCatalogProfileDetail(Model model,
                                             @RequestParam("courseId") BigDecimal courseId) {

        Map<String, Object> course = courseService.findCourseById(courseId, false, false, false, true);
        model.addAttribute("course", course);
        List<Map<String, Object>> catalogList = courseCatalogService.findCourseCatalogByCourseId(courseId, false, false, false, false);
        model.addAttribute("catalogList", catalogList);
        return resultPage("/course/profile/course-profile-detail-list");
    }


}
